package com.dingpeikeji.quickdeer.modules.rentproduct.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.FRentproductDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.RentproductProcedureDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TRentproductDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.ProRentOrders;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;

@Service
@Transactional(readOnly = true)
public class RentproductOrderService {
	@Autowired
	private TRentproductDao rentproductDao;
	@Autowired
	private FRentproductDao fRentproductDao;
	@Autowired
	private RentproductProcedureDao rentproductProcedureDao;
	
	public Page<TRentproduct> findRentproductPage(Page<TRentproduct> page, TRentproduct tRentproduct) {
		tRentproduct.setPage(page);
		page.setList(rentproductDao.findList(tRentproduct));
		return page;
	}
	
	/**
	 * 整包车查询
	 * @param tRentproduct
	 * @return
	 */
	public List<TRentproduct> findTRentproductList(TRentproduct tRentproduct){
		return rentproductDao.findList(tRentproduct);
	}
	/**
	 * 生成订单
	 */
	public ProRentOrders genOrder(TOrders orders) throws Exception{
		
		ProRentOrders pro = new ProRentOrders();
		try {
			pro.setOrdername(orders.getOrdername());
			pro.setOrdertype(orders.getOrdertype());
			pro.setCargotypeid(orders.getCargotypeid());
			if (orders.getOffice()==null) {
				pro.setOfficeids(orders.getOfficeids());
			}else{
				pro.setOfficeids(orders.getOffice().getId());
			}
			pro.setBoardaddr(orders.getBoardaddr());
			pro.setBookingdays(orders.getBookingdays());
			pro.setBookingtype(orders.getBookingtype());
			pro.setContacts(orders.getContacts());
			pro.setDepartdate(orders.getDepartdateStr());//2017-08-25 06:00
			pro.setDueamount(Double.valueOf(orders.getDueamount()));
			pro.setOffaddr(orders.getOffaddr());
			pro.setOrderuserid(orders.getOrderuserid());
			pro.setPayertel(orders.getPayertel());
			//接送机  realschid为long类型时间戳
			if (TOrders.ORDER_TYPE_AIR_SHARE.equals(orders.getOrdertype())||TOrders.ORDER_TYPE_RENTPRODUCT_AIRPLANE.equals(orders.getOrdertype())) {
				int intValue = new Long(orders.getRealscheid().substring(3)).intValue();
				pro.setProductid(intValue);
			}else {
				pro.setProductid(Integer.parseInt(orders.getRealscheid()));
			}
			pro.setPurchasenum(Integer.parseInt(orders.getPurchasenum()));
			pro.setRemarks(orders.getRemarks());
		} catch (NumberFormatException e) {
			throw new RuntimeException("订单信息中有不合法的数据，请检查");
		}
		if (TOrders.BOOKING_TYPE_NOT_FIRST_SHARE.equals(pro.getBookingtype())) {
			pro.setRealscheid(orders.getRealscheid());
			Map<String, Object> param = pro.toMap().getParam();
			rentproductProcedureDao.proRentOrders2(param);
		}else {
			Map<String, Object> param = pro.toMap().getParam();
			rentproductProcedureDao.proRentOrders(param);
		}
		return pro;
	}
}
